package com.max.project.graphics.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.max.common.proxy.ServiceProxyFactory;
import com.max.common.util.DataPage;
import com.max.project.graphics.model.Content;
import com.max.project.graphics.service.ContentService;


/**
 * 
 * 项目名称:max-project-graphics
 * 文件名称:ContentController.java
 * 文件描述:
 * 创建人员:chenyibo
 * 创建时间:2013-1-22 下午02:53:39
 * 修改人员:chenyibo
 * 修改时间:2013-1-22 下午02:53:39
 * 修改备注:
 * @version V1.0
 */
@Controller
public class ContentController 
{
	
	private static ContentService contentService = 
		new ServiceProxyFactory().create(ContentService.class);
	
	
	@RequestMapping({"/","/index"})
    public String index(Model model) 
		throws Exception
    {
        return list(1,20,model);
    }
	
	@RequestMapping("/list/{pageNo}/{pageSize}")
    public String list(@PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize, Model model) 
		throws Exception
    {
		DataPage<Content> dp = contentService.query(pageNo, pageSize);
		model.addAttribute("dp", dp);
		
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder
			      .getRequestAttributes()).getRequest();
		System.out.println(request.getRequestURL());
		
		return "list";
    }
	
	@RequestMapping("/view/{id}")
    public String view(@PathVariable("id") int id, Model model) 
		throws Exception
    {
		Content content = contentService.query(id);
		model.addAttribute("content", content);
		
		return "view";
    }
	
	@RequestMapping("/listJson/{pageNo}/{pageSize}")
    @ResponseBody  
    public Object listJson(@PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize) 
		throws Exception
    {
		DataPage<Content> dp = contentService.query(pageNo, pageSize);
		return dp;
    }
}
